var index = '0';
$(function () {
    if (!RssUser.Data.myid) {
        $('.mineText').text('未登录');
    } else {
        $('.mineText').text('我的');
    }
    $('.wrap-title>a').click(function(e) {
        index = e.target.dataset.index;
        if (index === '1') {
            $('.active-img').removeClass('hide');
            $('.active-img-r').addClass('hide');
            $(this).addClass('active');
            $('.index-list>div').addClass('gf-bg');
            $('.index-list>div').removeClass('xu-bg');
            $(this).siblings().removeClass('xu-active');
        } else {
            $('.active-img').addClass('hide');
            $('.active-img-r').removeClass('hide');
            $(this).addClass('xu-active');
            $('.index-list>div').addClass('xu-bg');
            $('.index-list>div').removeClass('gf-bg');
            $(this).siblings().removeClass('active');
        }
    })
    $('input[name=classfiyid]').val('1');
    getFirstActive('1');
    secondFloor('1');
    $('.nav-page-button').click(function (e) {
        const index = e.target.dataset.index;
        window.location.hash = '#index';
        changeFirst(index);
    })
    $('.wrap-addjixie-sss>div').click(function (e) {
        $(this).addClass('addjixie-active');
        $(this).siblings().removeClass('addjixie-active');
    })
});
$('#banzuPage').load(function () {
    $('.banzu-list').empty();
    RssApi.Table.List('zhaobanzu_classify').getJson(function (data) {
        console.info('data');
        let str = '';
        for (let i = 0; i < data.length ; i ++) {
            str += '<div onclick="tofabuBanzhuPages()" class="xu-bg">'+data[i].name+'</div>'
        }
        $('.banzu-list').append(str);
    })
})
function changeFirst(index) {
    getFirstActive(index)
    secondFloor(index);
}
function getFirstActive(index) {
    var zhaojixieclassify = window.rydata.zhaojixieclassify;
    var first = [];
    for (let j in zhaojixieclassify) {
        if(zhaojixieclassify[j][1] === '0') {
            first.push([zhaojixieclassify[j][0], j]);
        }
    }
    for (let j = 0; j < first.length; j++) {
        if (first[j][1] === index.toString()) {
            $('.select-text').text(first[j][0]);
            first[j] = first.splice(2, 1, first[j])[0];
        }
    }
    $('.scroll-first').empty();
    let firststr = '';
    for(let i = 0; i < first.length; i++) {
        firststr += '<div><div onclick="changeFirst('+first[i][1]+')" data-index='+first[i][1]+' class='+(first[i][1] === index.toString() ? "wrap-index-active" : '')+'>'+first[i][0]+'</div>'+(first[i][1] === index.toString() ?'<div class="active-tabs">当前选项</div>':'<img src='+(i < first.length/2 ? "./img/tmp/right1.png" : "./img/tmp/left2.png")+'>')+'</div>'
    }
    $('.scroll-first').append(firststr);
}
function toList(name, id) {
    if (!RssUser.Data.myid) {
        window.location.hash = '#loginpage';
        return;
    }
    if(window.location.hash && window.location.hash === '#ershouPage') {
        window.location.hash = '#hotPage'
    } else if(window.location.hash && window.location.hash === '#banzuPage') {
        window.location.hash = '#needList'
        $('#trigger1').text(name);
        $('input[name=classfiyid]').val(id);
        if(index === '1') {
            $('.gongfang').removeClass('hide');
            $('.xufang').addClass('hide');
            localStorage.typeid = 4;
        } else {
            $('.xufang').removeClass('hide');
            $('.gongfang').addClass('hide');
            $('.banzu-text').text('我的班组');
            localStorage.typeid = 3;
        }
    }else {
        if(index === '0') {
            $('#fabu1').text($('.select-text').text() + ' ' + name);
            $('.gongfang').removeClass('hide');
            $('.xufang').addClass('hide');
            localStorage.typeid = 2;
            window.location.hash = '#fabupage';
            $('input[name=classifyidf]').val(id);
        } else {
            $('#addjixie2').text($('.select-text').text() + ' ' + name);
            $('.xufang').removeClass('hide');
            $('.gongfang').addClass('hide');
            $('.banzu-text').text('我的机械');
            localStorage.typeid = 1;
            window.location.hash = '#addJixie';
            $('input[name=classifyidj]').val(id);
        }
    }
}
// 获得二级菜单
function secondFloor(id) {
    $('.index-list').empty();
    var second = [];
    var zhaojixieclassify = window.rydata.zhaojixieclassify;
    for (let j in zhaojixieclassify) {
        if(zhaojixieclassify[j][1] === id.toString()) {
            second.push([zhaojixieclassify[j][0], j]);
        }
    }
    let str = ''

    console.info(second, 'second');
    for(let i = 0; i < second.length; i++) {
        str += '<div data-id='+second[i][1]+' data-name='+second[i][0]+' class="xu-bg">'+second[i][0]+'</div>'
    }
    $('.index-list').append(str).find('div').click(function () {
        toList($(this).data('name'), $(this).data('id'));
    });
}
function toXU() {
    $('.xufang').removeClass('hide');
    $('.gongfang').addClass('hide');
    localStorage.typeid = 1;
    $('#trigger1').text('机械类型');
    $('input[name=classfiyid]').val('');
    $('input[name=location]').val('');
    location.hash = '#needList';
}
function tofabuBanzhuPages() {
    if (!RssUser.Data.myid) {
        window.location.hash = '#loginpage';
        return;
    }
    if (index === '0') {
        window.location.hash = '#fabuBanzhuPage';
    } else {
        localStorage.typeid = '3'
        window.location.hash = '#gfSavePage';
    }
}
function tofabuBanzhuPage() {
    if (!RssUser.Data.myid) {
        window.location.hash = '#loginpage';
        return;
    }
    window.location.hash = '#fabuBanzhuPage';
}
function togfSavePage() {
    if (!RssUser.Data.myid) {
        window.location.hash = '#loginpage';
        return;
    }
    localStorage.typeid = '3'
    window.location.hash = '#gfSavePage';
}